package com.generator.statics.manufacturerPurchaseStatics.service.impl;


import com.generator.statics.manufacturerPurchaseStatics.dao.PurchaseDao;
import com.generator.statics.manufacturerPurchaseStatics.pojo.CPurchase1;
import com.generator.statics.manufacturerPurchaseStatics.pojo.CPurchaseCon1;
import com.generator.statics.manufacturerPurchaseStatics.pojo.City1;
import com.generator.statics.manufacturerPurchaseStatics.pojo.Province1;
import com.generator.statics.manufacturerPurchaseStatics.service.PurchaseService;
import com.generator.util.PageModel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PurchaseServiceImpl implements PurchaseService {

    @Autowired
    private PurchaseDao purchaseDao;

    public PurchaseDao getPurchaseDao() {
        return purchaseDao;
    }

    public void setPurchaseDao(PurchaseDao purchaseDao) {
        this.purchaseDao = purchaseDao;
    }


    @Override
    public List<CPurchaseCon1> getPurchaseListByCondition(CPurchase1 purchase) {
        return purchaseDao.getPurchaseListByCondition(purchase);
    }

    @Override
    public PageModel<CPurchaseCon1> getPurchaseListByCondition(CPurchase1 purchase, PageModel<CPurchaseCon1> pageModel) {
        pageModel.setPageNum(pageModel.getPageNum() <= 0 ? 1 : pageModel.getPageNum());
        pageModel.setPageSize(pageModel.getPageSize() <= 0 ? 20 : pageModel.getPageSize());

        PageHelper.startPage(pageModel.getPageNum(), pageModel.getPageSize());

        List<CPurchaseCon1> list = purchaseDao.getPurchaseListByCondition(purchase);
        PageInfo<CPurchaseCon1> pageInfo = new PageInfo<CPurchaseCon1>(list);

        pageModel.setData(new ArrayList<CPurchaseCon1>(list));
        pageModel.setTotalPage(pageInfo.getPages());
        pageModel.setTotalRows(pageInfo.getTotal());

        return pageModel;
    }

    @Override
    public List<CPurchaseCon1> onePurchase(CPurchaseCon1 conn) {
        return purchaseDao.onePurchase(conn);
    }

    @Override
    public List<CPurchase1> classQueryPurchaseListByCondition(CPurchaseCon1 conn) {
        return purchaseDao.classQueryPurchaseListByCondition(conn);
    }

    @Override
    public PageModel<CPurchase1> classQueryPurchaseListByCondition(CPurchaseCon1 conn, PageModel<CPurchase1> pageModel) {
        pageModel.setPageNum(pageModel.getPageNum() <= 0 ? 1 : pageModel.getPageNum());
        pageModel.setPageSize(pageModel.getPageSize() <= 0 ? 20 : pageModel.getPageSize());

        PageHelper.startPage(pageModel.getPageNum(), pageModel.getPageSize());
        List<CPurchase1> userList = purchaseDao.classQueryPurchaseListByCondition(conn);
        PageInfo<CPurchase1> pageInfo = new PageInfo<CPurchase1>(userList);

        pageModel.setData(new ArrayList<CPurchase1>(userList));
        pageModel.setTotalPage(pageInfo.getPages());
        pageModel.setTotalRows(pageInfo.getTotal());
        System.out.println(userList.getClass());

        return pageModel;
    }

    @Override
    public String queryProvince(Province1 province) {
        return purchaseDao.queryProvince(province);
    }

    @Override
    public String queryCity(City1 city1) {
        return purchaseDao.queryCity(city1);
    }

    @Override
    public List<CPurchaseCon1> oneDetailedPurchase(CPurchaseCon1 conn) {
        return purchaseDao.oneDetailedPurchase(conn);
    }

    @Override
    public CPurchaseCon1 oneDetailedPurchaseDown(CPurchaseCon1 conn) {
        return purchaseDao.oneDetailedPurchaseDown(conn);
    }
}
